<template>
  <section>
    <!-- 背景颜色 -->
    <div class="color"></div>
    <div class="color"></div>
    <div class="color"></div>
    <div class="box">
      <!-- 背景圆 -->
      <div class="circle" style="--x:0"></div>
      <div class="circle" style="--x:1"></div>
      <div class="circle" style="--x:2"></div>
      <div class="circle" style="--x:3"></div>
      <div class="circle" style="--x:4"></div>
      <!-- 登录框 -->
      <div class="container">
        <div class="form">
          <h2>登录</h2>
          <form @submit.prevent="login">
            <div class="inputBox">
              <input
                  type="text"
                  placeholder="姓名"
                  v-model="loginData.username"
                  @blur="validateUsername"
              />
              <span v-if="errors.username" class="error">{{ errors.username }}</span>
            </div>
            <div class="inputBox">
              <input
                  type="password"
                  placeholder="密码"
                  v-model="loginData.password"
                  @blur="validatePassword"
              />
              <span v-if="errors.password" class="error">{{ errors.password }}</span>
            </div>
            <div class="inputBox">
              <input type="submit" value="登录" />
            </div>
            <p class="forget">忘记密码?<a href="#">点击这里</a></p>
            <p class="forget">没有账户?<a href="/register">注册</a></p>
          </form>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup>
import { ref } from 'vue'
import { userLoginService } from '@/api/user.js'
import { useTokenStore } from '@/stores/token.js'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

// 定义数据模型
const loginData = ref({
  username: '',
  password: '',
})

// 用来存储表单的错误信息
const errors = ref({
  username: '',
  password: '',
})

// 表单校验函数
const validateUsername = () => {
  if (!loginData.value.username) {
    errors.value.username = '请输入用户名'
  } else if (loginData.value.username.length < 5 || loginData.value.username.length > 16) {
    errors.value.username = '用户名长度应为5~16个字符'
  } else {
    errors.value.username = ''
  }
}

const validatePassword = () => {
  if (!loginData.value.password) {
    errors.value.password = '请输入密码'
  } else if (loginData.value.password.length < 5 || loginData.value.password.length > 16) {
    errors.value.password = '密码长度应为5~16个字符'
  } else {
    errors.value.password = ''
  }
}

// 登录函数
const login = async () => {
  // 进行校验
  validateUsername()
  validatePassword()

  // 如果有错误，则不进行登录
  if (errors.value.username || errors.value.password) {
    ElMessage.error('表单存在错误，请检查输入')
    return
  }

  try {
    // 调用登录接口
    const result = await userLoginService(loginData.value)

    // 登录成功，存储 token
    const tokenStore = useTokenStore()
    tokenStore.setToken(result.data)

    // 跳转到首页
    const router = useRouter()
    router.push('/main')
    ElMessage.success('登录成功！')
  } catch (error) {
    ElMessage.error('登录失败，请稍后再试')
    console.error(error)
  }
}
</script>

<style scoped src="@/assets/reg-log.css">
/* 输入框样式 */
.inputBox {
  margin-bottom: 20px;
  position: relative;
}

input {
  width: 100%;
  padding: 10px 20px;
  border-radius: 8px;
  border: 1px solid #ccc;
  font-size: 16px;
}

input[type="submit"] {
  background-color: #4CAF50;
  color: white;
  cursor: pointer;
}

input[type="submit"]:hover {
  background-color: #45a049;
}

/* 错误提示样式 */
.error {
  color: red;
  font-size: 12px;
  position: absolute;
  bottom: -18px;
  left: 0;
}

.forget {
  margin-top: 20px;
  text-align: center;
}

.forget a {
  color: #007bff;
  text-decoration: none;
}

.forget a:hover {
  text-decoration: underline;
}
</style>

